package com.sibco.client.common.mail;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

public class SendMail {
	public static final int CONTENTTYPE_HTML = 1;
	public static final int CONTENTTYPE_TEXT = 2;
	public static final int CONTENTTYPE_BOTH = 3;

	private String host;
	private String from;
	private String to;
	private String subject;
	private String text;
	private String mailer;
	private Session sendMailSession;
	private InternetAddress[] recipient;
	private Transport transport;
	private int ContentType = CONTENTTYPE_HTML;
	private String fromName;
	private String charset;
	private String enc;
	private String[][] files = new String[0][0];
	public SendMail() {
	}

	public void setHost(String host) {
		this.host = host;
	}

	public String getHost() {
		return host;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public String getFrom() {
		return from;
	}

	public void setTo(String to) {
		this.to = to;
	}

	public String getTo() {
		return to;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getSubject() {
		return subject;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getText() {
		return text;
	}

	public void setMailer(String mailer) {
		this.mailer = mailer;
	}

	public String getMailer() {
		return mailer;
	}

	public void setContentType(int ContentType) {
		this.ContentType = ContentType;
	}

	public int getContentType() {
		return ContentType;
	}

	public static void main(String args[]) throws UnsupportedEncodingException, MessagingException {
		
	}

	public void send() throws UnsupportedEncodingException, MessagingException {
			Properties props = System.getProperties();
			props.put("mail.smtp.host", host);

			Message msg = setEmailContext(props);
			// send the thing off

			//System.out.println("Message have been sent Mail.");

			Transport.send(msg);
		

	}
	private Message setEmailContext(Properties props) throws UnsupportedEncodingException, MessagingException {
		sendMailSession = Session.getDefaultInstance(props, null);
		Message msg = new MimeMessage(sendMailSession);
		msg.setFrom(new InternetAddress(from,fromName));

		msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
		msg.addRecipients(Message.RecipientType.CC,this.recipient);
		//subject = new String(subject.getBytes(),"GBK");
		// Adding a subject to the mail
		subject = MimeUtility.encodeText(new String(subject.getBytes(charset)),charset,enc);
		msg.setSubject(subject);
		// Adding the X-mailer to the mail (the name of the Mailer)
		msg.setHeader("X-Mailer", mailer);
		
		//Creating a multipart that will englobe all the already defined
		//bodyparts, this will also define the header of the msg
		Multipart multipart = new MimeMultipart();

		// Adding both, the text, and the signature

		// Adding the date to the mail
		switch (ContentType) {
			case CONTENTTYPE_HTML :
				{
					multipart.addBodyPart(addBody_HTML());
					break;
				}
			case CONTENTTYPE_TEXT :
				{
					multipart.addBodyPart(addBody_TEXT());
					break;
				}
			case CONTENTTYPE_BOTH :
				{
					multipart.addBodyPart(addBody_HTML());
					multipart.addBodyPart(addBody_TEXT());
					break;
				}
		}
		if(files!=null){
			for (int i = 0;i < files.length;i++){
				multipart.addBodyPart(addAttachment(files[i][0],files[i][1]));
			}
		}		
		msg.setSentDate(new Date());
		// Adding the mime multi part to the mail
		msg.setContent(multipart);

		return msg;

	}
	
	private MimeBodyPart addAttachment(String filePath,String fileName) throws UnsupportedEncodingException, MessagingException{
		MimeBodyPart multipartAtta = new MimeBodyPart();
		DataSource source = new FileDataSource(filePath);
		multipartAtta.setDataHandler(new DataHandler(source));
		multipartAtta.setFileName(MimeUtility.encodeText(new String(fileName.getBytes(charset)),charset,enc));
		return multipartAtta;
	}
	
	private MimeBodyPart addBody_HTML() throws MessagingException {
		// HTml
		MimeBodyPart multipartHTML = new MimeBodyPart();
		//
		//* text = new String(text.getBytes("8859_1"));
		multipartHTML.setText("" + text + "");
		// setting the right values for the Header
		multipartHTML.setHeader("Content-Type", "text/html;charset="+charset);
		multipartHTML.setHeader("Content-Transfer-Encoding", "quoted-printable");

		return multipartHTML;
	}

	private MimeBodyPart addBody_TEXT() throws MessagingException {
		//TEXT
		MimeBodyPart multitext = new MimeBodyPart();

		// This will add the text to the mime body part
		//* texto += "Text plain here";
		//* text = new String(text.getBytes("8859_1"));
		multitext.setText(text);

		// setting the right values for the Header
		multitext.setHeader("Content-Type", "text/plain;charset="+charset);
		multitext.setHeader("Content-Transfer-Encoding", "quoted-printable");
		// This will return all the fields to be signed
		//* ByteArrayOutputStream baos = new ByteArrayOutputStream();
		//* multitext.writeTo(baos);
		//* System.out.println("->>>>" + baos.toString()+"\n <------");
		return multitext;

	}

	public void sendByAuth(String Username, String Password) throws UnsupportedEncodingException {
		try {
			Properties props = System.getProperties();
			props.put("mail.smtp.host", host);
			props.put("mail.smtp.auth", "true");

			Message msg = setEmailContext(props);

			transport = sendMailSession.getTransport("smtp");
			transport.connect(host, Username, Password);
			transport.sendMessage(msg, msg.getAllRecipients()); //

			System.out.println("Message have been sent Mail by authentication.");

		} catch (MessagingException m) {
			m.printStackTrace();
			
		}
	}
	/**
	 * @return
	 */
	public InternetAddress[] getRecipient() {
		return recipient;
	}

	/**
	 * @param strings
	 */
	public void setRecipient(InternetAddress[] strings) {
		recipient = strings;
	}

	/**
	 * @return
	 */
	public String getFromName() {
		return fromName;
	}

	/**
	 * @param string
	 */
	public void setFromName(String string) {
		fromName = string;
	}

	/**
	 * @return
	 */
	public String getCharset() {
		return charset;
	}

	/**
	 * @param string
	 */
	public void setCharset(String string) {
		charset = string;
	}

	/**
	 * @return
	 */
	public String getEnc() {
		return enc;
	}

	/**
	 * @param string
	 */
	public void setEnc(String string) {
		enc = string;
	}

	/**
	 * @return
	 */
	public String[][] getFiles() {
		return files;
	}

	/**
	 * @param string
	 */
	public void setFiles(String[][] string) {
		files = string;
	}

}
